class Solution 
{
public:
    int dx[4]={0,0,1,-1};
    int dy[4]={1,-1,0,0};
    bool arr[301][301];
    int numIslands(vector<vector<char>>& g) 
    {
        int m=g.size();
        int n=g[0].size();
        int ret=0;
        for(int i=0;i<m;++i)
        {
            for(int j=0;j<n;++j)
            {
                if(g[i][j]=='1'&&arr[i][j]==false)
                {
                    ret++;
                    bfs(g,i,j,m,n);
                }

            }
        }
        return ret;
    }

    void bfs(vector<vector<char>>& g,int i,int j,int m,int n)
    {
        queue<pair<int,int>> q;;
        q.push({i,j});
        arr[i][j]=true;
        while(q.size())
        {
            auto [x,y]=q.front();
            q.pop();
            for(int i=0;i<4;++i)
            {
                int a=x+dx[i];
                int b=y+dy[i];
                if(a>=0&&a<m&&b>=0&&b<n&&arr[a][b]==false&&g[a][b]=='1')
                {
                    q.push({a,b});
                    arr[a][b]=true;
                }
            }
        }
    }
};
